package com.mall.server.service.oms;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dubbo.api.OmsOrderApi;
import com.mall.dubbo.api.OmsOrderItemApi;
import com.mall.dubbo.api.OmsOrderOperateHistoryApi;
import com.mall.model.domain.OmsOrder;
import com.mall.model.domain.OmsOrderItem;
import com.mall.model.domain.OmsOrderOperateHistory;
import com.mall.model.dto.OmsMoneyInfoDto;
import com.mall.model.dto.OmsOrderDeliveryDto;
import com.mall.model.dto.OmsOrderDto;
import com.mall.model.dto.OmsReceiverInfoDto;
import com.mall.model.enums.OrderStatus;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.OmsOrderDetailVo;
import com.mall.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author : xyiaao
 * @createDate : 2022/4/2 10:21
 */
@Service
public class OmsOrderService {

    @DubboReference
    private OmsOrderApi omsOrderApi;


    @DubboReference
    private OmsOrderOperateHistoryApi omsOrderOperateHistoryApi;


    @DubboReference
    private OmsOrderItemApi omsOrderItemApi;


    @Autowired
    private Redisson redisson;

    /**
     * 分页条件查询订单信息
     *
     * @param omsOrderDto
     * @return
     */
    public CommonPage<OmsOrder> page(OmsOrderDto omsOrderDto) {

        Page<OmsOrder> page = omsOrderApi.pageByOrderDto(omsOrderDto);
        return new CommonPage(page);
    }


    /**
     * 根据id获取订单详情
     *
     * @param id
     * @return
     */
    public OmsOrderDetailVo getOmsOrderDetailDtoByOrderId(Long id) {

        // 根据id查询订单信息
        OmsOrder omsOrder = omsOrderApi.getById(id);

        // 根据订单id查询操作记录
        List<OmsOrderOperateHistory> omsOrderOperateHistoryList = omsOrderOperateHistoryApi.listByOrderId(id);

        // 根据订单id查询商品信息
        List<OmsOrderItem> omsOrderItemList = omsOrderItemApi.listByOrderId(id);

        // 封装为订单详情并返回
        return OmsOrderDetailVo.init(omsOrder, omsOrderOperateHistoryList, omsOrderItemList);
    }

    public void delete(List<Long> ids) {

        omsOrderApi.removeByIds(ids);
        omsOrderOperateHistoryApi.deleteByOrderIds(ids);
    }

    public void updateClose(List<Long> ids, String note) {

        RLock lock = redisson.getLock(UserHolder.getId().toString());

        try {
            lock.lock(60, TimeUnit.SECONDS);
            omsOrderApi.updateClose(UserHolder.getId(), ids, OrderStatus.CLOSED.getValue(), note);
        } finally {
            lock.unlock();
        }
    }


    public void updateClose(Long id, String note) {

        RLock lock = redisson.getLock(UserHolder.getId().toString());

        try {
            lock.lock(60, TimeUnit.SECONDS);
            omsOrderApi.updateClose(UserHolder.getId(), id, OrderStatus.CLOSED.getValue(), note);
        } finally {
            lock.unlock();
        }
    }

    public void updateDelivery(List<OmsOrderDeliveryDto> deliveryParamList) {

        RLock lock = redisson.getLock(UserHolder.getId().toString());

        try {
            lock.lock(60, TimeUnit.SECONDS);
            omsOrderApi.updateDelivery(UserHolder.getId(), deliveryParamList, OrderStatus.SHIPPED.getValue());
        } finally {
            lock.unlock();
        }
    }

    public void updateNote(Long id, String note, Integer status) {

        RLock lock = redisson.getLock(UserHolder.getId().toString());

        try {
            lock.lock(60, TimeUnit.SECONDS);
            OmsOrder omsOrder = omsOrderApi.getById(id);
            omsOrder.setNote(note);
            omsOrderApi.updateById(omsOrder);
            omsOrderOperateHistoryApi.insertOmsOrderOperateHistory(UserHolder.getId(), omsOrder.getId(), status, "添加订单备注：" + note);
        } finally {
            lock.unlock();
        }
    }

    public void updateReceiverInfo(OmsReceiverInfoDto receiverInfoParam) {

        RLock lock = redisson.getLock(UserHolder.getId().toString());

        try {
            lock.lock(60, TimeUnit.SECONDS);
            OmsOrder omsOrder = omsOrderApi.getById(receiverInfoParam.getOrderId());
            BeanUtils.copyProperties(receiverInfoParam, omsOrder);
            omsOrderApi.updateById(omsOrder);
            omsOrderOperateHistoryApi.insertOmsOrderOperateHistory(UserHolder.getId(), omsOrder.getId(), receiverInfoParam.getStatus(), "修改收货人信息");
        } finally {
            lock.unlock();
        }
    }

    public void updateMoneyInfo(OmsMoneyInfoDto moneyInfoParam) {

        RLock lock = redisson.getLock(UserHolder.getId().toString());

        try {
            lock.lock(60, TimeUnit.SECONDS);
            OmsOrder omsOrder = omsOrderApi.getById(moneyInfoParam.getOrderId());
            BeanUtils.copyProperties(moneyInfoParam, omsOrder);
            omsOrderApi.updateById(omsOrder);
            omsOrderOperateHistoryApi.insertOmsOrderOperateHistory(UserHolder.getId(), omsOrder.getId(), moneyInfoParam.getStatus(), "修改订单费用信息");
        } finally {
            lock.unlock();
        }
    }

}
